﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpSphinx.Util.Configuration;

namespace SharpSphinx.Util.Configuration
{
    public class PropertySheet
    {
        public const string Comp_log_Level = "logLevel";


        private Dictionary<String, S4PropWrapper> registeredProperties = new Dictionary<string, S4PropWrapper>();
        private Dictionary<String, Object> propValues = new Dictionary<string, object>();

        

        private Dictionary<String, Object> rawProps = new Dictionary<string, object>();


        private ConfigurationManager cm;
        private IConfigurable owner;
        private IEnumerable<IConfigurable> OwnerClass;
        private String instanceName;

        public PropertySheet(IConfigurable configurable, String name, RawPropertyData rpd, ConfigurationManager ConfigurationManager)
            :  this(configurable.getClass(), name, ConfigurationManager, rpd)
        {
          
            this.instanceName = name;
            this.cm = ConfigurationManager; 
            this.owner = configurable;
            
        }


        public PropertySheet( IEnumerable<IConfigurable> confClass,String name , ConfigurationManager cm,RawPropertyData rpd)
         {
             OwnerClass = confClass;
             this.cm = cm;
             this.instanceName = name; 

             //parseClas 
             // setConf 

             Dictionary<String, Object> flatProp = rpd.flatten(cm).Properties;

             foreach (string propName in rawProps.Keys)
             {
                 object propValue = null;
                 flatProp.TryGetValue(propName,out propValue);
                 propValues.Add(propName, propValue);
             }
     
          }

        /// <summary>
        /// Registers a new property which type and default value are defined by the given sphinx property.
        /// </summary>
        /// <param name="propName">The name of the property to be registered.</param>
        /// <param name="property">The property annotation masked by a proxy.</param>
        private void registerProperty(string propName, S4PropWrapper property)
        {
            if (property == null || propName == null)
                return;  // throw new InternalConfigurationException()


            if (!registeredProperties.ContainsKey(propName))
                registeredProperties.Add(propName, property);


            if (!propValues.ContainsKey(propName))
            {

                propValues.Add(propName, null);
                rawProps.Add(propName, null);

            }

        }


        public S4PropWrapper getProperty(String name /*class<?> */)
        {
           if (! propValues.ContainsKey(name))
           {
               //throw new InternalConfigurationException()
               throw new ArgumentNullException();
               
           }

            S4PropWrapper S4Prop; 
            registeredProperties.TryGetValue(name,out S4Prop); 

          /* try {
              propertyClass

            }*/
                

            return S4Prop; 
        }


        private String FlattenProp(String name)
        {
            Object value = null; 
            propValues.TryGetValue(name, out value);
            return value is string ? (String)value : null; 
        }


        public int getInt(string name)
        {
            S4PropWrapper S4PropWra = getProperty(name); 
            //S4I

        }


        public float getFloat(String name)
        {
            return ((Double)getDouble(name)).floatValue();
        }

        public double getDouble(String naeme)
        {



        }
    }
}
